package com.autotest.glin.run;

import com.autotest.glin.common.RunCaseJson;
import com.autotest.glin.data.DataProviders;
import com.autotest.glin.util.Config;
import com.alibaba.fastjson.JSONObject;
import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.assertEquals;

public class course_count_search {
    @BeforeClass
    public void setUp() throws IOException {
        Map<String, Object> confMap = Config.getCaseConf("course_count_search");
        RestAssured.baseURI = (String) confMap.get("url");
        RestAssured.basePath = (String) confMap.get("path");
        RestAssured.port = Integer.valueOf(confMap.get("port").toString());
    }

    @Test(dataProvider = "dataprovider", dataProviderClass = DataProviders.class, description = "course_count_search")
    public void runCase(String caseMeta, String params) {
        String postbody = JSONObject.parseObject(params).getJSONObject("data").getString("jsonbody");
        Header header = new Header("accessToken", "c02d20da-e03d-4c5b-8f93-47f0c5761abd");
        List<Header> headersList = new ArrayList<Header>();
        headersList.add(header);
        Headers headers = new Headers(headersList);
        Response response = RunCaseJson.runCase(postbody, headers);
        response.prettyPrint();
        String resString = response.asString();
        JSONObject resJsonObject = JSONObject.parseObject(resString);
        System.out.println(resJsonObject.getString("errorMsg"));
        JSONObject caseMetaJsonObject = JSONObject.parseObject(caseMeta);
        assertEquals(caseMetaJsonObject.getString("preResult"), resJsonObject.getString("errorMsg"));
    }

    public static void main(String[] args) throws IOException {
        Map<String, Object> confMap = Config.getCaseConf("course_count_search");
        System.out.println(confMap.get("url"));
    }
}
